/*
 ============================================================================
 Name		: AutoTesterAppUi.cpp
 Author	  : 
 Copyright   : Your copyright notice
 Description : CAutoTesterAppUi implementation
 ============================================================================
 */

// INCLUDE FILES
#include <avkon.hrh>
#include <aknmessagequerydialog.h>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <f32file.h>
#include <s32file.h>
#include <hlplch.h>

#include <AutoTester_0xE447FCF4.rsg>

#include "AutoTester_0xE447FCF4.hlp.hrh"
#include "AutoTester.hrh"
#include "AutoTester.pan"
#include "AutoTesterApplication.h"
#include "AutoTesterAppUi.h"
#include "AutoTesterAppView.h"

#include "mtalog.h"

_LIT(KFileName, "C:\\private\\E447FCF4\\AutoTester.txt");
_LIT(KText, "Hello World!");

// ============================ MEMBER FUNCTIONS ===============================


// -----------------------------------------------------------------------------
// CAutoTesterAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CAutoTesterAppUi::ConstructL()
{
	// Initialise app UI with standard value.
	BaseConstructL(CAknAppUi::EAknEnableSkin);

	// Create view object
	iAppView = CAutoTesterAppView::NewL(ClientRect() );

	AddToStackL(iAppView); // Important
	
	// Create a file to write the text to
	TInt err = CCoeEnv::Static()->FsSession().MkDirAll(KFileName);
	if ( (KErrNone != err) && (KErrAlreadyExists != err))
	{
		return;
	}

	RFile file;
	err = file.Replace(CCoeEnv::Static()->FsSession(), KFileName, EFileWrite);
	CleanupClosePushL(file);
	if (KErrNone != err)
	{
		CleanupStack::PopAndDestroy(1); // file
		return;
	}

	RFileWriteStream outputFileStream(file);
	CleanupClosePushL(outputFileStream);
	outputFileStream << KText;

	CleanupStack::PopAndDestroy(2); // outputFileStream, file
	iAutoTester = CAutoTester::GetInstance();
	iGlobalNote = CGlobalNote::NewL();
//    HideApplicationFromFSW();
}
// -----------------------------------------------------------------------------
// CAutoTesterAppUi::CAutoTesterAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CAutoTesterAppUi::CAutoTesterAppUi()
{
	// No implementation required
}

// -----------------------------------------------------------------------------
// CAutoTesterAppUi::~CAutoTesterAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CAutoTesterAppUi::~CAutoTesterAppUi()
{
	if (iAppView)
	{
		RemoveFromStack(iAppView);
		delete iAppView;
		iAppView = NULL;
	}
	if(iAutoTester)
	{
		delete iAutoTester;
		iAutoTester = NULL;
	}
	if(iGlobalNote)
	{
		delete iGlobalNote;
		iGlobalNote = NULL;
	}
}

// -----------------------------------------------------------------------------
// CAutoTesterAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//
void CAutoTesterAppUi::HandleCommandL(TInt aCommand)
{
	switch (aCommand)
	{
	case EEikCmdExit:
	case EAknSoftkeyExit:
		Exit();
		break;

	case EStart:
	{
		iAutoTester->Start();
	}
	break;
	case EStop:
	{
		iAutoTester->Stop();
	}
	break;
	case EReplay:
	{
		iAutoTester->Replay();
	}
	break;
	case ESave:
	{
		iAutoTester->SaveEvents();
	}
	break;
	case EReload:
	{
		if(iAutoTester->GetArrayCount() != 0)
		{
			iGlobalNote->ShowGlobalNoteL();
		}
		else
		{
			iAutoTester->ReloadEvents();
		}
	}
	break;
	case EScheduleReplay:
	{
	}
	break;
	case EOneHour:
	{
		if(iAutoTester->GetArrayCount() != 0)
		{
			iAutoTester->ScheduleReplay(1);
		}
		else
		{
			CAknGlobalNote* note = CAknGlobalNote::NewL();
			CleanupStack::PushL(note);
			note->ShowNoteL(EAknGlobalInformationNote, _L("No events to be scheduled!"));
			CleanupStack::PopAndDestroy(1);
		}
	}
	break;
	case ETwoHours:
	{
		if(iAutoTester->GetArrayCount() != 0)
		{
			iAutoTester->ScheduleReplay(2);
		}
		else
		{
			CAknGlobalNote* note = CAknGlobalNote::NewL();
			CleanupStack::PushL(note);
			note->ShowNoteL(EAknGlobalInformationNote, _L("No events to be scheduled!"));
			CleanupStack::PopAndDestroy(1);
		}
	}
	break;
	case EFourHours:
	{
		if(iAutoTester->GetArrayCount() != 0)
		{
			iAutoTester->ScheduleReplay(4);
		}
		else
		{
			CAknGlobalNote* note = CAknGlobalNote::NewL();
			CleanupStack::PushL(note);
			note->ShowNoteL(EAknGlobalInformationNote, _L("No events to be scheduled!"));
			CleanupStack::PopAndDestroy(1);
		}
	}
	break;
	case ESixHours:
	{
		if(iAutoTester->GetArrayCount() != 0)
		{
			iAutoTester->ScheduleReplay(6);
		}
		else
		{
			CAknGlobalNote* note = CAknGlobalNote::NewL();
			CleanupStack::PushL(note);
			note->ShowNoteL(EAknGlobalInformationNote, _L("No events to be scheduled!"));
			CleanupStack::PopAndDestroy(1);
		}
	}
	break;
	case EAbout:
	{
		CAknMessageQueryDialog* dlg = new (ELeave)CAknMessageQueryDialog();
        dlg->PrepareLC(R_ABOUT_QUERY_DIALOG);
        HBufC* title = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TITLE);
        dlg->QueryHeading()->SetTextL(*title);
        CleanupStack::PopAndDestroy(); //title
        HBufC* msg = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TEXT);
        dlg->SetMessageTextL(*msg);
        CleanupStack::PopAndDestroy(); //msg
        dlg->RunLD();
	}
	break;
	default:
		Panic(EAutoTesterUi);
		break;
	}
}
// -----------------------------------------------------------------------------
//  Called by the framework when the application status pane
//  size is changed.  Passes the new client rectangle to the
//  AppView
// -----------------------------------------------------------------------------
//
void CAutoTesterAppUi::HandleStatusPaneSizeChange()
{
	iAppView->SetRect(ClientRect() );
}

CArrayFix<TCoeHelpContext>* CAutoTesterAppUi::HelpContextL() const
{
#warning "Please see comment about help and UID3..."
	// Note: Help will not work if the application uid3 is not in the
	// protected range.  The default uid3 range for projects created
	// from this template (0xE0000000 - 0xEFFFFFFF) are not in the protected range so that they
	// can be self signed and installed on the device during testing.
	// Once you get your official uid3 from Symbian Ltd. and find/replace
	// all occurrences of uid3 in your project, the context help will
	// work.
	CArrayFixFlat<TCoeHelpContext>* array = new(ELeave)CArrayFixFlat<TCoeHelpContext>(1);
	CleanupStack::PushL(array);
	array->AppendL(TCoeHelpContext(KUidAutoTesterApp, KGeneral_Information));
	CleanupStack::Pop(array);
	return array;
}

// End of File
